// ----------------------------------------------------------------------
//  Jank McCaffery
//  11-30-2014
// ----------------------------------------------------------------------

#include "topicModel/boundedParameters.H"
#include "topicModel/multinomialParameters.H"

#include <QtTest/QtTest>

#include <iostream>
using namespace std;


/// \brief test class for basicData class
class MultinomialParameters_test : public QObject
{

Q_OBJECT

private slots:

	/// \brief Initialize data for testing
	void initTestCase ();

	/// \brief Test general parameters functionality
	void testGradient ();

	/// \brief Cleanup after testing
	void cleanupTestCase ();

private:
	/// \brief Test parameters
	MultinomialParameters<double> m;

	/// \brief Test parameters
	MultinomialParameters<double> n;

	/// \brief Gradient to apply
	FixedParameters<double> gradient;
};

void MultinomialParameters_test::initTestCase()
{
	m.getData ().resize (2, 4);

	m[0][0] = 0.1;
	m[0][1] = 0.2;
	m[0][2] = 0.3;
	m[0][3] = 0.4;
	m[1][0] = 0.0;
	m[1][1] = 0.2;
	m[1][2] = 0.3;
	m[1][3] = 0.5;

	n.getData ().resize (2, 4);

	n[0][0] = 0.1;
	n[0][1] = 0.2;
	n[0][2] = 0.3;
	n[0][3] = 0.4;
	n[1][0] = 0.1;
	n[1][1] = 0.2;
	n[1][2] = 0.3;
	n[1][3] = 0.4;

	gradient.getData ().resize (2, 4);

	gradient[0][0] = -1.2;
	gradient[0][1] = 1.4;
	gradient[0][2] = 1.8;
	gradient[0][3] = 1;
	gradient[1][0] = -1.2;
	gradient[1][1] = 1.4;
	gradient[1][2] = 1.8;
	gradient[1][3] = 1;
}

void MultinomialParameters_test::testGradient()
{
	MultinomialParameters<double> test;

	test = m;

	test.applyGradient (gradient, 0);

	QVERIFY (test == m);

	test = m;
	test.applyGradient (gradient, 1);

	QVERIFY (qFuzzyCompare (test[0][0] + 1, 1));
	QVERIFY (qFuzzyCompare (test[0][1] + 1, 1));
	QVERIFY (qFuzzyCompare (test[0][2], 0.35));
	QVERIFY (qFuzzyCompare (test[0][3], 0.65));
	QVERIFY (qFuzzyCompare (test[1][0] + 1, 1));
	QVERIFY (qFuzzyCompare (test[1][1] + 1, 1));
	QVERIFY (qFuzzyCompare (test[1][2], 0.3));
	QVERIFY (qFuzzyCompare (test[1][3], 0.7));

	test = n;
	test.applyGradient (gradient, 1);

	QVERIFY (qFuzzyCompare (test[0][0] + 1, 1));
	QVERIFY (qFuzzyCompare (test[0][1] + 1, 1));
	QVERIFY (qFuzzyCompare (test[0][2], 0.35));
	QVERIFY (qFuzzyCompare (test[0][3], 0.65));
	QVERIFY (qFuzzyCompare (test[1][0] + 1, 1));
	QVERIFY (qFuzzyCompare (test[1][1] + 1, 1));
	QVERIFY (qFuzzyCompare (test[1][2], 0.35));
	QVERIFY (qFuzzyCompare (test[1][3], 0.65));
}

void MultinomialParameters_test::cleanupTestCase ()
{

}


QTEST_MAIN (MultinomialParameters_test)
#include "multinomialParameters_test.moc"
